package karma.main;

import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Random;

import net.minecraft.client.Minecraft;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.GuiButton;
import net.minecraft.src.GuiScreen;
import net.minecraft.src.GuiTextField;
import net.minecraft.src.ModLoader;
import net.minecraft.src.RenderEngine;
import net.minecraft.src.World;
import org.lwjgl.opengl.GL11;

public class GuiTeleAmulet extends GuiScreen
{
    private BufferedImage img;
    private int imgID;
    private GuiTextField field;
    public static int selected = 1;
    boolean allowtp = false;
    static boolean save = false;
    public static boolean channeling = false;
    public static int channeltimer = 0;
    
    private static double c = 0;
    private static double c1 = 0;
    private static double c2 = 0;
    private static EnumTeleportLocation etl;

    static Random rand;

    public GuiTeleAmulet()
    {
        imgID = 1000;
        rand = new Random();
    }

    /**
     * Adds the buttons (and other controls) to the screen in question.
     */
    public void initGui()
    {
        try
        {
            img = ModLoader.loadImage(mc.renderEngine, "/karma/guibkgrnd.png");
            mc.renderEngine.setupTexture(img, imgID);
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }

        controlList.clear();
        field = new GuiTextField(fontRenderer, width / 2 - 78, height / 2 - 44, 156, 20);
        field.setMaxStringLength(24);
        field.setFocused(true);
        controlList.add(new GuiButton(1, width / 2 - 78, height / 2 - 19, 45, 20, "#1"));
        controlList.add(new GuiButton(2, width / 2 - 23, height / 2 - 19, 45, 20, "#2"));
        controlList.add(new GuiButton(3, width / 2 + 32, height / 2 - 19, 45, 20, "#3"));
        controlList.add(new GuiButton(4, width / 2 - 78, height / 2 + 6, 45, 20, "#4"));
        controlList.add(new GuiButton(5, width / 2 - 23, height / 2 + 6, 45, 20, "#5"));
        controlList.add(new GuiButton(6, width / 2 + 32, height / 2 + 6, 45, 20, "#6"));
        controlList.add(new GuiButton(7, width / 2 - 78, height / 2 + 31, 45, 20, "#7"));
        controlList.add(new GuiButton(8, width / 2 - 23, height / 2 + 31, 45, 20, "#8"));
        controlList.add(new GuiButton(9, width / 2 + 32, height / 2 + 31, 45, 20, "#9"));
        controlList.add(new GuiButton(11, width / 2 - 78, height / 2 + 56, 156, 20, "Save Location"));
        controlList.add(new GuiButton(12, width / 2 - 78, height / 2 + 81, 156, 20, "Teleport"));
    }

    /**
     * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e).
     */
    public void actionPerformed(GuiButton guibutton)
    {
        Minecraft minecraft = ModLoader.getMinecraftInstance();
        World world = minecraft.theWorld;
        net.minecraft.src.EntityPlayerSP entityplayersp = minecraft.thePlayer;

        if (guibutton.id < 10)
        {
            selected = guibutton.id;
            field.setText("");
        }

        if (guibutton.id == 11)
        {
            saveLocation(minecraft, world, entityplayersp);
        }

        if (guibutton.id == 12)
        {
            teleportToLocation(minecraft, world, entityplayersp);
        }
    }

    /**
     * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e).
     */
    public void keyTyped(char c, int i)
    {
        super.keyTyped(c, i);
        field.textboxKeyTyped(c, i);
    }

    /**
     * Called when the mouse is clicked.
     */
    public void mouseClicked(int i, int j, int k)
    {
        super.mouseClicked(i, j, k);
        field.mouseClicked(i, j, k);
    }

    /**
     * Returns true if this GUI should pause the game when it is displayed in single-player
     */
    public boolean doesGuiPauseGame()
    {
        return false;
    }

    public static EnumTeleportLocation getSelected()
    {
        if (selected == 1)
        {
            return EnumTeleportLocation.l1;
        }

        if (selected == 2)
        {
            return EnumTeleportLocation.l2;
        }

        if (selected == 3)
        {
            return EnumTeleportLocation.l3;
        }

        if (selected == 4)
        {
            return EnumTeleportLocation.l4;
        }

        if (selected == 5)
        {
            return EnumTeleportLocation.l5;
        }

        if (selected == 6)
        {
            return EnumTeleportLocation.l6;
        }

        if (selected == 7)
        {
            return EnumTeleportLocation.l7;
        }

        if (selected == 8)
        {
            return EnumTeleportLocation.l8;
        }
        else
        {
            return EnumTeleportLocation.l9;
        }
    }

    public void saveLocation(Minecraft minecraft, World world, EntityPlayer entityplayer)
    {
        GuiTeleAmulet _tmp = this;
        EnumTeleportLocation enumteleportlocation = getSelected();

        if (field.getText() != "" && field.getText() != null)
        {
            enumteleportlocation.locname = field.getText();
        }

        enumteleportlocation.xpos = entityplayer.posX;
        enumteleportlocation.ypos = entityplayer.posY - 1.5;
        enumteleportlocation.zpos = entityplayer.posZ;
        entityplayer.addChatMessage((new StringBuilder()).append("Location ").append((int)enumteleportlocation.xpos).append(", ").append((int)enumteleportlocation.ypos).append(", ").append((int)enumteleportlocation.zpos).append(" saved as ").append(enumteleportlocation.locname).append(".").toString());
        save = true;
    }

    public void teleportToLocation(Minecraft minecraft, World world, EntityPlayer entityplayer)
    {
        GuiTeleAmulet _tmp = this;
        EnumTeleportLocation enumteleportlocation = getSelected();
        double d = enumteleportlocation.xpos;
        double d1 = enumteleportlocation.ypos;
        double d2 = enumteleportlocation.zpos;

        if (d == 0.0D && d1 == 0.0D && d2 == 0.0D)
        {
            entityplayer.addChatMessage("Cannot teleport to 0, 0, 0!");
        }
        else
        {
            this.allowtp = ItemTeleAmulet.allowTP(mc.thePlayer, d, d1, d2);
            if(allowtp)
            {
            	mc.thePlayer.closeScreen();
            	c = d;
            	c1 = d1;
            	c2 = d2;
            	etl = enumteleportlocation;
            	this.channelTP();
            }
            else
            {
            	mc.thePlayer.closeScreen();
            	entityplayer.addChatMessage("Not enough magic powder to teleport!");
            }
        }
    }

    public static void channelTP() 
    {
    	double d = c;
    	double d1 = c1;
    	double d2 = c2;
    	EnumTeleportLocation enumteleportlocation = etl;
    	EntityPlayer entityplayer = ModLoader.getMinecraftInstance().thePlayer;
    	
    	if (!channeling)
    	{
    		channeling = true;
    		channeltimer = 0;
    		GuiKarmaHUD.played = false;
    	}
    	if (channeltimer == 25 && channeling)
    	{
    		entityplayer.setPositionAndUpdate(d, d1, d2);
    		entityplayer.addChatMessage((new StringBuilder()).append("Teleported to ").append(enumteleportlocation.locname).append("!").toString());
    		for (int var2 = 0; var2 < 32; ++var2)
    		{
    			entityplayer.worldObj.spawnParticle("portal", d, d1 + rand.nextDouble() * 2.0D, d2, rand.nextGaussian(), 0.0D, rand.nextGaussian());
    		}
    		entityplayer.worldObj.playSoundEffect(d, d1, d2, "mob.endermen.portal", 1.0F, 1.0F);
    		channeling = false;
    	}
	}

	/**
     * Draws the screen and all the components in it.
     */
    public void drawScreen(int i, int j, float f)
    {
        drawDefaultBackground();

        try
        {
            int k = mc.renderEngine.getTexture("/karma/guibkgrnd.png");
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            mc.renderEngine.bindTexture(k);
            drawTexturedModalRect(width / 2 - 88, height / 2 - 111, 0, 0, 176, 222);
        }
        finally { }
        int dis = ItemTeleAmulet.getTotalDistance(mc.thePlayer, getSelected().xpos, getSelected().ypos, getSelected().zpos);
        String cost;
        if (dis <= 100)
        {
        	cost = "Free";
        }
        else
        {
        	cost = "" + (int)dis / 100 + " MP";
        }
        
        GuiTeleAmulet _tmp = this;
        int l = (int)getSelected().xpos;
        GuiTeleAmulet _tmp1 = this;
        int i1 = (int)getSelected().ypos;
        GuiTeleAmulet _tmp2 = this;
        int j1 = (int)getSelected().zpos;
        GuiTeleAmulet _tmp3 = this;
        String s2 = ("Distance: " + dis + ", Cost: " + cost);
        String s = getSelected().locname;
        String s1 = (new StringBuilder()).append("").append(l).append(", ").append(i1).append(", ").append(j1).toString();
        drawCenteredString(fontRenderer, s1, width / 2, height / 2 - 81, 0xffffff);
        drawCenteredString(fontRenderer, s, width / 2, height / 2 - 101, 0xffffff);
        if (getSelected().ypos != 0 && getSelected().xpos != 0 && getSelected().zpos != 0)
        {
        	drawCenteredString(fontRenderer, s2, width / 2, height / 2 - 61, 0xffffff);
        }
        else
        {
        	drawCenteredString(fontRenderer, "Cannot TP to 0, 0, 0", width / 2, height / 2 - 61, 0xffffff);
        }
        field.drawTextBox();
        super.drawScreen(i, j, f);
    }
}
